package practise.alogrithm.gotoSiliconValley.array;

import java.util.Arrays;

/**
 * find out if the array contains any element equal to the expected result,.
 */
public class SumOfTwoNumberI {
    public boolean hasSum(int[] array, int target){
        int length = array.length;

        if(array==null || length<2){
            return false;
        }
        //sort the array, there the complexity is o(nlogn)
        Arrays.sort(array);

        for(int start =0, end = array.length-1;start<end;){
            int sum = array[start]+array[end];
            if(sum==target){
                return true;
            }
            else if(sum<target){
                start++;
            }
            else
                end--;
        }
        return false;
    }
}
